﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using GD.ModelContest.Service;
using GD.ModelContest.Model;
using GD.ModelContest.Common;

namespace GD.ModelContest.Web.Admin
{
    public partial class ListNews : BaseCheckPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataBind_NewsCollection(0);
            }
        }

        protected void DataBind_NewsCollection(int recordcount)
        {
            if (recordcount == 0)
            {
                this.AspNetPager_FH.CurrentPageIndex = 1;
            }

            int count = recordcount;
            int pageIndex = this.AspNetPager_FH.CurrentPageIndex;
            int pageSize = this.AspNetPager_FH.PageSize;

            int newTypeId = Convert.ToInt32(this.ddlNewsType.SelectedValue);
            this.rpNewsCollection.DataSource = ServiceLocator.INewsService.GetNewsCollection(newTypeId, pageIndex, pageSize, ref count);
            this.rpNewsCollection.DataBind();
            this.AspNetPager_FH.RecordCount = count;
        }

        protected void rpNewsCollection_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                mc_news article = (mc_news)e.Item.DataItem;

                Label lblId = (Label)e.Item.FindControl("lblId");
                lblId.Text = article.newsId.ToString();

                Label lblSort = (Label)e.Item.FindControl("lblSort");
                if (article.newsTypeId == 1)
                {
                    lblSort.Text = "赛事公告";
                }
                else
                {
                    lblSort.Text = "新闻速递";
                }

                Label lblTitle = (Label)e.Item.FindControl("lblTitle");
                lblTitle.Text = article.title;

                Label lblCreateTime = (Label)e.Item.FindControl("lblCreateTime");
                lblCreateTime.Text = article.creatTime.ToString("yyyy-MM-dd");

                HyperLink HyperLink_Edit = (HyperLink)e.Item.FindControl("HyperLink_Edit");
                HyperLink_Edit.NavigateUrl = string.Format(HyperLink_Edit.NavigateUrl, article.newsId);

                LinkButton lkb_Delete = (LinkButton)e.Item.FindControl("lkb_Delete");
                lkb_Delete.CommandArgument = Convert.ToString(article.newsId);
            }
        }

        protected void AspNetPager_FH_PageChanged(object sender, EventArgs e)
        {
            DataBind_NewsCollection(this.AspNetPager_FH.RecordCount);
        }

        protected void rpNewsCollection_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int Id = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "OperateDelete")//删除
            {
                if (ServiceLocator.INewsService.DeleteNews(Id))
                {
                    DataBind_NewsCollection(0);
                }
                else
                {
                    JavaScript.Alert("对不起！删除失败！", this.Page);
                    return;
                }
            }
        }

        protected void lkbSearch_Click(object sender, EventArgs e)
        {
            DataBind_NewsCollection(0);
        }
    }
}
